import yblist from "../yblist/yblist.vue";
import qs from "qs"
import headerComm from "../header-comm/header.vue"
import { Number } from "core-js";

export default {
  name: "tree1",
  data() {
    return {
      //树上--单选框
      radiovalue: [],
      radiov: {
        value: '',
        value1: "",
        value2: "",
        ybGroupId: ""
      },
      radioStyle: {

        display: 'inline-block',
        height: '30px',
        lineHeight: '30px',
      },
      //树上--单选框

      filterText: "",
      dataSource: [],
      indexsou: "",
      isEdit: false,
      treeData: [],
      openExpandAll: false,//默认不展开所有树节点
      add_question_flag: false,

      new_question_name: "",

      tree_key: 0,

      defaultExpand: [],

      ybgllist: false,

      value: "",
      restaurants: [],
      state: '',
      //树形结构
      leftdatalist: [],
      from: {
        fenleiID: "",
        ids: ""
      },
      partit: "",
      chiltit: "",
      sysAppIds: ""
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  components: {
    yblist,
    headerComm,
  },
  methods: {
    
    //树上--单选框
    radiotree(e) {
      
      this.radiov.ybGroupId = e.target.value
      this.getTreeStructure()
    },

    handleNodeClick(data) {

      if (data.ids == '0') {

        this.partit = data.title

      }
      if (data.ids != '0') {

        this.chiltit = data.title
      }

      this.$store.state.datatitle = this.partit + this.chiltit

      this.$store.state.treeid = data.id
      this.$store.state.fenleiId = data.fenleiID
      this.$store.state.ids = data.ids
      this.showHierarchy = false;

      this.$store.state.exflage = true
      this.$store.commit('getSampleDetail')
      this.$store.commit('getybsy')

    },
    routerto(key, data) {
    },

    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 添加新大题

    add_new_question() {
      this.add_question_flag = true;
    },

    // 添加父节点------------
    add_question_sure(data) {
      //确定
      if (this.new_question_name == "") {
        this.$notification['error']({
          message: '警告',
          description: '名称不能为空',
        });
      } else {
        const nodeObj = {
          key: "",
          label: this.new_question_name,
          isEdit: false,
          children: [],
          fenleiID: data.fenleiID,
        };


        this.treeData.push(nodeObj);
        this.add_question_flag = false;
        this.axios.post("yangben/addSampleEntry.php", qs.stringify({ filename: this.$store.state.filename, cnName: nodeObj.label, ybGroupId: this.radioStyle.value })).then(res => {
          if (res.data.code == 303) {
            this.getTreeStructure()

          }
        })

      }
    },
    // 添加父节点------------

    // 取消添加父节点------------
    add_question_cancel() {
      //取消

      this.add_question_flag = false;

      this.new_question_name = "";
    },
    // 取消添加父节点------------

    //      增加
    append(store, node, data) {
      var maxid = "20";
      //新增数据
      const nodeapp = {
        children: [],
        key: ++maxid,
        label: "增加节点",
        isEdit: false,
        fenleiID: data.fenleiID,
        ids: this.from.ids
      };

      // data.children.push(nodeapp);
      if (!node.expanded) {
        node.expanded = true;
      }

      this.axios.post("yangben/addTreeMenu.php", qs.stringify({ filename: this.$store.state.filename, cnName: nodeapp.label, fenleiId: data.fenleiID, ids: data.ids }))
        .then(res => {


          if (res.data.code == 300) {
            // this.from.ids = res.data.id
            nodeapp.ids = res.data.id
            data.children.push(nodeapp);
            //  this.getTreeStructure()

          }
        })
    },

    // 修改节点名称
    nodeEdit(ev, store, data) {
      data.isEdit = true;
      this.$nextTick(() => {
        const $input =
          ev.target.parentNode.parentNode.querySelector("input") ||
          ev.target.parentElement.parentElement.querySelector("input");
        !$input ? "" : $input.focus();


      });
    },

    edit_sure(ev, data) {
      const $input =
        ev.target.parentNode.parentNode.querySelector("input") ||
        ev.target.parentElement.parentElement.querySelector("input");

      if (!$input) {
        return false;
      } else {

        this.axios.post("yangben/modifySampleName.php", qs.stringify({ filename: this.$store.state.filename, name: $input.value, fenleiID: data.fenleiID, ids: data.ids })).then(res => {

          data.label = $input.value;
        })


        data.isEdit = false;
      }
    },


    // 节点删除
    nodeDelete(node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = data.ids == 0 ? children.findIndex(d => d.fenleiID === data.fenleiID) : children.findIndex(d => d.ids === data.ids);
      this.axios.post("yangben/deleteSampleMenu.php", qs.stringify({
        filename: this.$store.state.filename,
        fenleiId: children[index].fenleiID,
        ids: children[index].ids
      })).then(res => {
        if (res.data.code == 303) {
          children.splice(index, 1);
        }
      })
    },




    // 节点上移
    nodeUp(node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const cIndex = children.findIndex(d => d.id === data.id)
      if (parent.level === 0 && cIndex === 0) {
        return;
      }
      else if (parent.level !== 0 && cIndex === 0) {
        return;
      }
      else if ((parent.level === 0 && cIndex !== 0) || (parent.level !== 0 && cIndex !== 0)) {
        const tempChildrenNodex1 = children[cIndex - 1]
        const tempChildrenNodex2 = children[cIndex]
        this.$set(children, cIndex - 1, tempChildrenNodex2)
        this.$set(children, cIndex, tempChildrenNodex1)
        this.defaultExpand = []
        if (parent.level === 0 && cIndex !== 0) { }
        else { this.defaultExpand[0] = parent.data.id }

        this.axios.post("yangben/UpSortSample.php", qs.stringify({ filename: this.$store.state.filename, fenleiId: data.fenleiID, ids: data.ids, pid: data.pid })).then(res => {
          if (res.data.code == 303) {
            this.tree_key++;
          }
        })
      }
    },


    // 节点下移
    nodeDown(store, node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const cIndex = children.findIndex(d => d.id === data.id)
      const cLength = children.length - 1; // 最边上的节点
      const allLevel = store.length - 1; // 树的深度
      if (parent.level === allLevel && cIndex === cLength) {
        return;
      }

      else if ((parent.level === allLevel && cIndex !== cLength) || (parent.level !== allLevel && cIndex !== cLength)) {
        const tempChildrenNodex1 = children[cIndex + 1];
        const tempChildrenNodex2 = children[cIndex];
        this.$set(children, cIndex + 1, tempChildrenNodex2);
        this.$set(children, cIndex, tempChildrenNodex1);
        this.defaultExpand = []
        if (parent.level != 0) {
          this.defaultExpand[0] = parent.data.id
        }

        this.axios.post("yangben/DownSortSample.php", qs.stringify({ filename: this.$store.state.filename, fenleiId: data.fenleiID, ids: data.ids, pid: data.pid })).then(res => {
          if (res.data.code == 303) {
            this.tree_key--
          }
        })
      }
    },


    showOrEdit(data) {
      if (data.isEdit) {
        return (

          <input
            type="text" style="height: 20px;"
            οncοpy="return true"
            value={data.label}
            on-blur={ev => this.edit_sure(ev, data)}
          />
        );
      } else {
        return <span className="node_labe">{data.label}</span>;
      }
    },
    //拖拽完成后触发的时间
    handleDrop(before, after, dropType, ev) {
      const ids = dropType == 'before' && after.data.pid == 0 ? 0 : after.data.ids
      this.axios.post("yangben/MoveSampleMenu.php", qs.stringify({ filename: this.$store.state.filename, fenleiId: after.data.fenleiID, ids: ids, nowIds: before.data.ids })).then(res => {
      })
    },
    //判断节点是否允许拖拽
    allowdrag(node) {
      return node.level != 1
    },
    // 结构树操作group node,

    renderContent(h, { node, data, store }) {
      return (
        <span>
          <span class="el-icon-document">
            {this.showOrEdit(data)}
          </span>

          <div class="tree_node_op">

            <i
              class="el-icon-edit"
              on-click={ev => this.nodeEdit(ev, store, data)}
            ></i>

            <a-popconfirm
              class="el-icon-delete"
              title="确定删除吗?"
              on-confirm={() => this.nodeDelete(node, data)}
            ></a-popconfirm>

            <i
              class="el-icon-upload2"
              on-click={() => this.nodeUp(node, data)}
            ></i>

            <i
              class="el-icon-download"
              on-click={() => this.nodeDown(store, node, data)}
            ></i>
            <i
              class="el-icon-plus"
              on-click={() => this.append(store, node, data)}
            ></i>
          </div>
        </span>
      );
    },

    //tree--添加分类的输入框
    onchangevalue(value) {
      console.log(this.new_question_name)

      console.log(`selected ${value}`);
      this.new_question_name = value
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toUpperCase().indexOf(input.toUpperCase()) >= 0
    },

    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },

    //复制样本分类数据
    copyTreeData() {
      this.openExpandAll = !this.openExpandAll
      //合并

      //展开
      if (this.openExpandAll) {

        for (var i = 0; i < this.treeData.length; i++) {
          // this.defaultExpand.push(this.treeData[i].id)
          if (this.treeData[i].children.length) {
            for (var ii = 0; ii < this.treeData[i].children.length; ii++) {
              this.defaultExpand.push(this.treeData[i].children[ii].id)
            }
          }
        }
      }
      else {
        this.defaultExpand = []
        //this.defaultExpand=[]
      }

      console.log('ssssss', this.defaultExpand);

    },

    //获取树形控件数据
    getTreeStructure() {

      this.axios.post("yangben/GetTreeStructure.php",
        qs.stringify({
           filename: this.$store.state.filename == null ? this.$store.state.bbl_sqdwid : this.$store.state.filename, 
           dwmc: this.$store.state.dwmc == null ? sessionStorage.getItem("dwmc") : this.$store.state.dwmc, 
           ybGroupId: this.radiov.ybGroupId 
          })).then(res => {
          console.log(res)
          if(res.data.menu.length){
            this.treeData = res.data.menu

            this.$store.state.fenleiId = res.data.menu[0].fenleiID
            this.$store.state.ids = res.data.menu[0].ids
            this.radioStyle.value = parseInt(res.data.ybGroupId)
            this.radiovalue = res.data.ybGroupArr
            this.radiov.ybGroupId = res.data.ybGroupId
          }
        



        })
    }

  },


  created() {
    this.getTreeStructure()
    this.$store.commit('getdatalist')
    console.log(this.$parent.$parent.collapsed)

    setTimeout(() => {
      this.ybgllist = true;

    }, 1000);

  }
};